﻿@{
	Layout = null;
}

<link href="~/libs/layui-v2.9.21/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />

<div style="margin-top:20px;margin-left:20px;">
	<form class="layui-form">
		<div class="layui-form-item">
			<div class="layui-inline">
				<label>物料编码</label>
				<div class="layui-inline">
					<input id="materialCode" style="width:200px;" class="layui-input" />
				</div>
			</div>
			<div class="layui-inline">
				<label>物料名称</label>
				<div class="layui-inline">
					<input id="materialName" style="width:200px;" class="layui-input" />
				</div>
			</div>
			<div class="layui-inline">
				<div class="layui-inline">
					<button type="button" id="searchmaterial" class="layui-btn layui-bg-blue">查询</button>
				</div>
			</div>
			<div class="layui-inline">
				<div class="layui-inline">
					<button class="layui-btn" type="button" style="margin-right:1000px;" id="addBtn" lay-on="test-page">+新增</button>
				</div>
			</div>
		</div>
	</form>
</div>
<div>
	<div id="ID-tree-demo" style="width:200px;float:left"></div>
	<div style="float:left;width:1300px;height:800px;">
		<table id="demo" lay-filter="test" style="margin-top:10px;"></table>
	</div>
</div>

<script type="text/html" id="toolDemo">
	<div class="layui-clear-space">
	  <button type="button" class="layui-btn layui-btn-xs" lay-event="edit">编辑</button>
	  <button type="button" class="layui-btn layui-bg-red layui-btn-xs" lay-event="delws">删除</button>
	</div>
</script>

<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.9.21/layui-v2.9.21/layui/layui.js"></script>

<script>
	layui.use(['table','layer','form','tree','dropdown'],function(){
		var tree = layui.tree;
		var layer = layui.layer;
		var util = layui.util;
		var form=layui.form;
		var table=layui.table;
		var dropdown=layui.dropdown;

		$(function(){
			//初始化绑定树形
			inittrees()
			Getmarialsdatas()
		})
		var treeId=0;

		function inittrees(){
			$.ajax({
				url:'/Puffs/GetMatertypeTrees',
				data:{orderby:0},
				type:'get',
				dataType:"json",
				success:function(res){
					tree.render({
						elem: '#ID-tree-demo',
						data:res.data,
						showLine: false,  // 是否开启连接线
						accordion:true,
						click: function(obj){
								treeId=obj.data.id;
						// Getmarialsdatas()
							searchtrees()
					   }
					});
				}
			})
		}

		function searchtrees(){
			//查询
			$('#searchmaterial').on('click',function(){
				var materialCode=$('#materialCode').val();
				var materialName=$('#materialName').val();
				table.reload('demo',{
					where:{
						materialCode:materialCode,
						materialName:materialName,
						id:treeId
					}
				});
		})
		}

		function Getmarialsdatas(){
			table.render({
				elem:'#demo',
				url:'/Puffs/GetMaterialList',
				page:true,
				limit:10,
				limits:[10,15,20],
				cols:[[
						{field:'materialCode',title:'物料编码',align:'center'},
						{field:'materialName',title:'物料名称',align:'center'},
						{field:'specificationsModels',title:'规格型号',align:'center'},
						{field:'unit',title:'单位',align:'center'},
						{field:'products',title:'产品列表',align:'center'},
						{field:'materialTypeName',title:'物料类型',align:'center'},
						{field:'state',title:'是否启用',align:'center'},
						{field:'safetyStock',title:'安全库存',align:'center'},
						{fixed: 'right', title:'操作', width: 134, minWidth: 125, templet: '#toolDemo'}
				]],
				parseData:function(res){
					console.log(res)
				},
				done:function(res,curr,count){
					console.log(res,curr,count)
				}
			})
		}

		//触发单元格工具事件
		table.on('tool(test)',function(obj){
			layEvent=obj.event;
			var data=obj.data;//获取当前行数据
			if(layEvent==='delws'){
				layer.confirm('确定删除该条信息吗？',function(index){
					$.ajax({
						url:'/Puffs/DeleteMatermangements',
						type:'post',
						data:{
							id:data.id
						},
						dataType:'json',
						async:false,
						success:res=>{
							if(res.code==0){
								layer.msg('删除成功')
								table.reload('GetMaterialList')
							}else{
								layer.msg('删除失败')
							}
						}
					})
				})
			}else if(lay.event==='edit'){
				layer.open({
				  type: 2,
				  title: "编辑",
				  shade: [0.6],
				  area: ['800px', '500px'],
				  offset: ['0px', '200px'],
				  shift: 2,
				  content: ['/Puffs/EditMaterialView?id='+data.id, 'no'],
				  end: function () {

				  }
			    });
			}
		})
	})

	//新增弹框
		layui.use(function(){
		  var $ = layui.$;
		  var layer = layui.layer;
		  var util = layui.util;
		  // 事件
		  util.on('lay-on', {
			'test-page': function(){
			  layer.open({
				type: 2,
				area: ['800px', '700px'],
				title:'新增',
				skin: 'layui-layer-rim', // 加上边框
				content: ['/puffs/AddMaterialView', 'no'] // 数组第二个成员设为 no 即屏蔽 iframe 滚动条
			  });
			},
		  })
		});

</script>